In ActionScript 3.0, displayObject instances have mouseX/mouseY properties; MouseEvent instance have localX/localY properties and stageX/stageY properties.
If there are other displayObject instances on stage and when cursor rolls over any of them, mouseX/mouseY becomes relative to that instance as opposed to relative to stage. To get the mouse coordinates relative to the stage, use stageX/stageY properties.
Or use localToGlobal method of displayObject class. Like this:
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.geom.Point;
//var _foo:TextField;
function onMouseMove(evt:MouseEvent){
//trace(evt.target.mouseY);
var target:* = evt.target;
//_foo.text = target.mouseY;
//_foo.text = evt.localY.toString();
var location:Point = new Point(target.mouseX, target.mouseY);
location = target.localToGlobal(location);
_foo.text = target.mouseY + ": "+ location.y;}
//root.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
August 28, 2007 at 10:30 am |
er, can’t you use the MouseEvent.stageX and stageY properties?
September 2, 2007 at 9:10 pm |
chihilatte, Thanks for the lead!;)
March 27, 2008 at 1:54 am |
stage.addEventListener(MouseEvent.CLICK,func);
function func(e:MouseEvent):void
{
trace(e.target.mouseX);
trace(e.target.mouseY);
}
November 19, 2010 at 6:28 am |
simply awesome…!!!
February 21, 2011 at 6:30 pm |
Excellent
December 21, 2008 at 9:04 pm |
Chihilatte :
You can get the MouseEvent.stageX and stageY properties, sure, but you have to wait until the user uses the mouse (moves it, clicks it, w/e).
December 15, 2009 at 4:29 am |
Hi there, nice tutorial tnx ๐
March 16, 2010 at 6:28 pm |
you can use
target.stage.mouseX
target.stage.mouseY
works like a charm
March 16, 2010 at 6:31 pm |
just modifying Gauravs example alittle bit, so that it doesnt matter if you have any objects on top of the stage when you mouse over.
stage.addEventListener(MouseEvent.CLICK,func);
function func(e:MouseEvent):void
{
trace(e.target.stage.mouseX);
trace(e.target.stage.mouseY);
}
June 27, 2010 at 4:30 am |
Hi, how would you put the mouse x, y coordinates into a Dynamic Text field instead of tracing it? sorry if the answer to this is obvious but I’m really new to AS3.
August 2, 2010 at 7:01 pm
Create 2 dynamic text fields on the stage called mouseX_txt and mouseY_txt.
put on your actions frame.
done deal!
by the way i wouldnt of been able to solve this without the previos post thank you all!
stage.addEventListener(Event.ENTER_FRAME, mouseyPos);
function mouseyPos (e:Event):void
{
var mouseXpos:Number = e.target.stage.mouseX;
var mouseYpos:Number = e.target.stage.mouseY;
mouseX_txt.text = String(mouseXpos);
mouseY_txt.text = String(mouseYpos);
}
December 26, 2011 at 4:28 pm |
you must be a king.
thanks bro, i really could not get it.
it taken my 2 hour ๐
May 25, 2010 at 2:17 am |
Hello,
just wanna say thanks for this great blog entry as well as the most helpful comments here.
๐
August 2, 2010 at 6:52 pm |
Create 2 dynamic text fields on the stage called mouseX_txt and mouseY_txt.
put on your actions frame.
done deal! ๐
by the way i wouldnt of been able to solve this without the previos post thank you all!
stage.addEventListener(Event.ENTER_FRAME, mouseyPos);
function mouseyPos (e:Event):void
{
var mouseXpos:Number = e.target.stage.mouseX;
var mouseYpos:Number = e.target.stage.mouseY;
mouseX_txt.text = String(mouseXpos);
mouseY_txt.text = String(mouseYpos);
}
August 2, 2010 at 6:53 pm |
by the way thats for the co-ords to appear in the text fields.
October 24, 2010 at 2:46 am |
when to use e.stageX
and when to use just mouseX
March 18, 2011 at 8:00 pm |
Great tutorial–just what I needed!
December 22, 2011 at 7:42 am |
//create dynamic text “_foo” and try this
function ifmove(evt:MouseEvent) {
_foo.text = stage.mouseX + “: “+ stage.mouseY;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, ifmove);
December 30, 2011 at 1:24 pm |
promovare prin facebook…
[…]Getting mouse coordinates on stage « :maohao:[…]…
October 30, 2012 at 1:12 pm |
Hi, my name is David and I have an AS3 question.
I’ve got a flying sheep on my stage which is draggable (this works). It hovers above a globe. What I want is when I drag the sheep to the left half of the stage, the globe starts turning clockwise, and when the sheep is on the right half the globe has to start turning counter clockwise. So, if it works it will create the illusion that the sheep is flying and that the user has the power to move the sheep around the globe.
PLEAAAASSSEEE, I hope someone can help me out! I can’t find anything about this topic on the internet…